package com.sthz.facepay.controller;

import com.sthz.facepay.enums.ApiResultEnum;
import com.sthz.facepay.model.Agent;
import com.sthz.facepay.model.AgentRes;
import com.sthz.facepay.model.result.ApiResult;
import com.sthz.facepay.service.AgentService;
import com.sthz.facepay.util.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author luli
 * @version jdk 1.8
 * @date 2019/7/16 14:41
 */
@Controller
@RequestMapping(value = "/api/agent")
public class AgentController {

    @Autowired
    AgentService agentService;

    /**
     * 查询代理商列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult findAll() {
        List<Agent> list = agentService.findAll();
        return ApiResultUtil.getApiResult(list, ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
    }

    /**
     * 根据代理商名称模糊查询
     * @return name
     */
    @ResponseBody
    @RequestMapping(value = "/queryListByName", method = RequestMethod.POST)
    public ApiResult queryAgentList(String name) {
        List<Agent> list = agentService.selectAgentByName(name);
        return ApiResultUtil.getApiResult(list, ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
    }

    /**
     *  新增代理商
     * @return agent
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult save(Agent agent) {
        // 若密码为空，则默认123456
        if(StringUtils.isEmpty(agent.getPassword())){
            agent.setPassword("123456");
        }
        int code = agentService.save(agent);
        if (code == 10000) {
            return ApiResultUtil.getApiResult(ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
        }
        return ApiResultUtil.getApiResult(ApiResultEnum.FAIL.getErrCode(),ApiResultEnum.FAIL.getErrMsg());
    }

    /**
     *  根据ID修改代理商信息
     * @return agent
     */
    @ResponseBody
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ApiResult update(Agent agent) {
        if(StringUtils.isEmpty(agent.getId())){
            return ApiResultUtil.getApiResult(ApiResultEnum.FAIL.getErrCode(),"ID丢失");
        }
        int code = agentService.modify(agent);
        if (code == 10000) {
            return ApiResultUtil.getApiResult(ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
        }
        return ApiResultUtil.getApiResult(ApiResultEnum.FAIL.getErrCode(),ApiResultEnum.FAIL.getErrMsg());
    }

    /**
     * 总代理商数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/agentStatistics", method = RequestMethod.POST)
    public ApiResult agentStatistics() {
        AgentRes agentRes = agentService.agentStatistics();
        return ApiResultUtil.getApiResult(agentRes,ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
    }
}
